package com.xgimi.open.skill.datamanager.biz.utils;

import java.io.*;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.zip.GZIPOutputStream;

/**
 * Created by xiaoxiong.sun on 2022/6/6.
 */
public class TextFileWriter {
    public static void writeGZipFile(String fileName, List<String> arrayList) {

        BufferedWriter bufferedWriter = null;
        try {
            FileOutputStream output = new FileOutputStream(fileName);

            Writer writer = new OutputStreamWriter(new GZIPOutputStream(output), "UTF-8");


            bufferedWriter = new BufferedWriter(writer);
            for (String arrayListItem : arrayList) {
                bufferedWriter.write(arrayListItem);
                bufferedWriter.write("\n");
            }
            bufferedWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
            }
        }
    }

    public static void writeLines(String fileName, Collection<String> lines) {

        File file = new File(fileName);
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(file));
            for (String line : lines) {
                if (line == null) {
                    line = "null";
                }
                bufferedWriter.write(line);
                bufferedWriter.write("\n");
            }
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
            }
        }
    }

    public static void writeLines(String fileName, List<String> lines, boolean appendMode) {
        if (!appendMode) {
            writeLines(fileName, lines);
            return;
        }


        File file = new File(fileName);
//		if(file.exists()){
//
//		}
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(file, true));
            for (String line : lines) {
                if (line == null) {
                    line = "null";
                }
                bufferedWriter.write(line);
                bufferedWriter.write("\n");
            }
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
            }
        }
    }

    public static void writeLines(String fileName, Set<String> lines, String encoding) {
        BufferedWriter bufferedWriter = null;
        //OutputStreamWriter out=null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileName), encoding));
            for (String line : lines) {
                if (line == null) {
                    line = "null";
                }
                bufferedWriter.write(line);
                if (!line.endsWith("\n")) bufferedWriter.write("\n");
            }
            bufferedWriter.close();

        } catch (UnsupportedEncodingException e) {
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static void writeLines(String fileName, Set<String> lines) {
        File file = new File(fileName);
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(file));
            for (String line : lines) {
                if (line == null) {
                    line = "null";
                }
                bufferedWriter.write(line);
                if (!line.endsWith("\n")) bufferedWriter.write("\n");
            }
            bufferedWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
            }
        }
    }
}

